package cn.bellychang.ood.litejunit.v3.framework.notification;

import cn.bellychang.ood.litejunit.v3.framework.runner.Description;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author ChangLiang
 * @date 2020/5/27
 */
public class Failure {

    private final Description fDescription;
    private Throwable fThrownException;

    /**
     * Constructs a <code>Failure</code> with the given description and exception.
     * @param description a <code>Description</code> of the test that failed
     * @param thrownException the exception that was thrown while running the test
     */
    public Failure(Description description, Throwable thrownException) {
        fThrownException = thrownException;
        fDescription= description;
    }

    /**
     * @return a user-understandable label for the test
     */
    public String getTestHeader() {
        return fDescription.getDisplayName();
    }

    /**
     * @return the exception thrown
     */

    public Throwable getException() {
        return fThrownException;
    }

    @Override
    public String toString() {
        StringBuffer buffer= new StringBuffer();
        buffer.append(getTestHeader() + ": "+fThrownException.getMessage());
        return buffer.toString();
    }

    /**
     * Convenience method
     * @return the printed form of the exception
     */
    public String getTrace() {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        getException().printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        return buffer.toString();
    }

    /**
     * Convenience method
     * @return the message of the thrown exception
     */
    public String getMessage() {
        return getException().getMessage();
    }
}
